﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class RegisterPanel : BasePanel
{
    public TMP_InputField userField;
    public TMP_InputField passwordField;

    public Button cancelBtn;
    public Button confirmBtm;


    protected override void Init()
    {
        cancelBtn.onClick.AddListener(() =>
        {
            userField.text = "";
            passwordField.text = "";
            UIManager.Instance.HidePanel<RegisterPanel>();
            UIManager.Instance.ShowPanel<LoginPanel>();
        });

        confirmBtm.onClick.AddListener(() => 
        {
            if (userField.text.Length <= 6 || passwordField.text.Length <= 6)
            {
                userField.text = "";
                passwordField.text = "";
                UIManager.Instance.ShowPanel<TipPanel>().ChangeInfo("用户名和密码必须大于6位");
                return;
            }

            if (!LoginManager.Instance.RegisterUser(userField.text, passwordField.text))
            {
                userField.text = "";
                passwordField.text = "";
                UIManager.Instance.ShowPanel<TipPanel>().ChangeInfo("用户已经存在");
                return;
            }

            LoginManager.Instance.ClearLoginData();
            UIManager.Instance.ShowPanel<LoginPanel>().ChangeUserAndPW(userField.text,passwordField.text);
            UIManager.Instance.HidePanel<RegisterPanel>();

        });
    }
}
